/*
 *Copyright 2014 DDPush
 *Author: AndyKwok(in English) GuoZhengzhu(in Chinese)
 *Email: ddpush@126.com
 *

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/
package org.ddpush.im.v1.node;

import java.net.SocketAddress;
import java.nio.ByteBuffer;

import org.ddpush.im.util.StringUtil;
import org.ddpush.im.v1.node.ClientStatMachine;

/**
 * 客户端消息包类 </br>
 * 跟这个类类似{@link org.ddpush.im.v1.client.appuser.Message}
 */
public final class ClientMessage {

	/**
	 * socket套接字地址
	 */
	protected SocketAddress address;
	/**
	 * 消息数据
	 */
	protected byte[] data;

	public ClientMessage(SocketAddress address, byte[] data) throws Exception {
		this.address = address;
		this.data = data;
	}

	// public static org.ddpush.im.node.Message getNewInstance(){
	// return null;
	// }

	/**
	 * 设置、修改消息包数据体
	 * 
	 * @param data
	 */
	public void setData(byte[] data) {
		this.data = data;
	}

	/**
	 * 获取消息包具体的消息数据
	 * 
	 * @return
	 */
	public byte[] getData() {
		return this.data;
	}

	/**
	 * 获得套接字地址
	 * 
	 * @return
	 */
	public SocketAddress getSocketAddress() {
		return this.address;
	}

	/**
	 * 设置消息包的套接字地址
	 * 
	 * @param addr
	 */
	public void setSocketAddress(SocketAddress addr) {
		this.address = addr;
	}

	/**
	 * 获得到消息数据的version
	 * 
	 * @return
	 */
	public int getVersionNum() {
		byte b = data[0];
		return b & 0xff;
	}

	/**
	 * 获得消息包得类型 0 心跳，16 通用消息 17 分类消息 32 自定义消息
	 * 
	 * @return
	 */
	public int getCmd() {
		byte b = data[2];
		return b & 0xff;
	}

	/**
	 * 获取消息包中消息数据的长度
	 * 
	 * @return
	 */
	public int getDataLength() {
		return (int) ByteBuffer.wrap(data, 19, 2).getChar();
	}

	/**
	 * 获取uuid
	 * 
	 * @return
	 */
	public String getUuidHexString() {
		return StringUtil.convert(data, 3, 16);
	}

	/**
	 * 检测消息数据包格式是否正确
	 * 
	 * @return
	 */
	public boolean checkFormat() {
		if (this.data == null) {
			return false;
		}
		// 消息包长度检测
		if (data.length < Constant.CLIENT_MESSAGE_MIN_LENGTH) {
			return false;
		}
		// 消息包版本检测
		if (getVersionNum() != Constant.VERSION_NUM) {
			return false;
		}
		// 消息类型检测
		int cmd = getCmd();
		if (cmd != ClientStatMachine.CMD_0x00
				// && cmd != ClientStatMachine.CMD_0x01
				&& cmd != ClientStatMachine.CMD_0x10 && cmd != ClientStatMachine.CMD_0x11
				&& cmd != ClientStatMachine.CMD_0x20 && cmd != ClientStatMachine.CMD_0xff) {
			return false;
		}
		// 消息长度检测
		int dataLen = getDataLength();
		// 消息类型和消息内容长度格式检测
		if (data.length != dataLen + Constant.CLIENT_MESSAGE_MIN_LENGTH) {
			return false;
		}

		if (cmd == ClientStatMachine.CMD_0x00 && dataLen != 0) {
			return false;
		}

		if (cmd == ClientStatMachine.CMD_0x10 && dataLen != 0) {
			return false;
		}

		if (cmd == ClientStatMachine.CMD_0x11 && dataLen != 8) {
			return false;
		}

		if (cmd == ClientStatMachine.CMD_0x20 && dataLen != 0) {
			return false;
		}

		return true;
	}

	// public byte[] getUUID(){
	// return
	// }

}
